<?php
class Formi_Attr_Setter
{
    private static $instance;

    private function __construct() {}

    public static function instance()
    {
        if(is_null(self::$instance))
            self::$instance = new Formi_Attr_Setter();

        return self::$instance;
    }

    public function set_element_attributes($element, $options)
    {
        if(!is_array($options))
            return;

        try
        {
            if(!$element instanceof Formi)
                $this->set_element_name($element, $options);

            $this->set_element_id($element, $options);
            $this->set_element_class($element, $options);
        }
        catch(Kohana_Exception $e)
        {
            Kohana::log('error', $e->getMessage());
        }
    }

    private function set_element_name($element, $options)
    {
        if(array_key_exists('name', $options))
            $element->attributes['name'] = $options['name'];
        else
            throw new Kohana_Exception('Element must have a name');
    }

    private function set_element_id($element, $options)
    {
        if(array_key_exists('id', $options))
            $element->attributes['id'] = $options['id'];
    }

    private function set_element_class($element, $options)
    {
        if(array_key_exists('class', $options))
            $element->attributes['class'] = $options['class'];
    }
}